<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\User;
use DB;
class PersoninfoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = Auth::user();
        $userinfo = DB::table('usersinfo')->where('uid',$user->id)->first();
        // dd($userinfo);
        return view('home.person_info')->with('user',$user)->with('userinfo',$userinfo);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if($img=$request->file('image')){
           $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
           $path = public_path('upload/images/');
           $name = time().rand(10,100).'x.'.$ext;
           if($img->move($path,$name)){
               $url = '/upload/images/'.$name;
            }

        }else{
            $url = $request->photo;
        }
               // 插主表
                $user = User::where('id',$id)->update([
                    'name'=>$request->name,
                    'nickname'=>$request->nickname,
                    'mobile'=>$request->mobile,
                    'email'=>$request->email
                ]);

                //插入附表
                $userinfo = DB::table('usersinfo')->where('uid',$id)->update([
                    'sex'=>$request->sex,
                    'image'=>$url
                ]);

            return redirect('/personinfo');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
